package com.b2c.oa.controller;

import com.b2c.oa.DataConfigObject;
import com.ims.entity.LiveDataReportEntity;
import com.ims.entity.MediaAccountEntity;
import com.ims.service.LiveDataReportService;
import com.ims.service.MediaAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping(value = "/live")
public class LiveDataController {
    @Autowired
    private MediaAccountService mediaAccountService;
    @Autowired
    private LiveDataReportService reportService;
    /**
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "/data_report", method = RequestMethod.GET)
    public String list(Model model, HttpServletRequest request) {

        String page = request.getParameter("page");
        Integer pageIndex = 1;
        Integer pageSize = DataConfigObject.getInstance().getPageSize();
        Integer mediaAccountId = 0;
        if (!StringUtils.isEmpty(request.getParameter("pageSize"))) {
            try {
                pageSize = Integer.parseInt(request.getParameter("pageSize"));
            } catch (Exception e) {
            }
        }
        if (!StringUtils.isEmpty(request.getParameter("accountId"))) {
            try {
                mediaAccountId = Integer.parseInt(request.getParameter("accountId"));
                model.addAttribute("accountId",mediaAccountId);
            } catch (Exception e) {
            }
        }

        var result = reportService.getList(pageIndex, pageSize, mediaAccountId);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("pageSize", pageSize);
        model.addAttribute("totalSize", result.getTotalSize());
        model.addAttribute("lists", result.getList());

        //查询客户列表
        List<MediaAccountEntity> mediaList = mediaAccountService.getList(1);
        model.addAttribute("mediaList", mediaList);

        model.addAttribute("menuId", "live_data_report");
        return "live_data_report";

    }

    @RequestMapping(value = "/data_report_m", method = RequestMethod.GET)
    public String mList(Model model, HttpServletRequest request) {

        String page = request.getParameter("page");
        Integer pageIndex = 1;
        Integer pageSize = DataConfigObject.getInstance().getPageSize();
        Integer mediaAccountId = 0;
        if (!StringUtils.isEmpty(request.getParameter("pageSize"))) {
            try {
                pageSize = Integer.parseInt(request.getParameter("pageSize"));
            } catch (Exception e) {
            }
        }
        if (!StringUtils.isEmpty(request.getParameter("accountId"))) {
            try {
                mediaAccountId = Integer.parseInt(request.getParameter("accountId"));
                model.addAttribute("accountId",mediaAccountId);
            } catch (Exception e) {
            }
        }

        var result = reportService.getList(pageIndex, pageSize, mediaAccountId);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("pageSize", pageSize);
        model.addAttribute("totalSize", result.getTotalSize());
        model.addAttribute("lists", result.getList());

//        //查询客户列表
//        List<MediaAccountEntity> mediaList = mediaAccountService.getList(1);
//        model.addAttribute("mediaList", mediaList);

        model.addAttribute("menuId", "live_data_report");
        return "live_data_report_m";

    }

    /**
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "/data_input", method = RequestMethod.GET)
    public String add(Model model, HttpServletRequest request) {
        model.addAttribute("report", new LiveDataReportEntity());
        //查询客户列表
        List<MediaAccountEntity> mediaList = mediaAccountService.getList(1);
        model.addAttribute("mediaList", mediaList);

        model.addAttribute("menuId", "live_data_input");
        return "live_data_report_input";

    }

    @RequestMapping(value = "/data_input", method = RequestMethod.POST)
    public String addPost(Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        Object userId = request.getSession().getAttribute("userId");

        Integer mediaAccountId = Integer.parseInt(request.getParameter("mediaAccountId"));

        String liveDate= request.getParameter("liveDate");
        String startTime= request.getParameter("startTime");
        String endTime= request.getParameter("endTime");

        Integer viewCount = Integer.parseInt(request.getParameter("viewCount"));
        Integer newUser = Integer.parseInt(request.getParameter("newUser"));
        Integer commentCount = Integer.parseInt(request.getParameter("commentCount"));

        //流量来源
        Integer source1 = Integer.parseInt(request.getParameter("source1"));
        Integer source2 = Integer.parseInt(request.getParameter("source2"));
        Integer source3 = Integer.parseInt(request.getParameter("source3"));
        Integer source4 = Integer.parseInt(request.getParameter("source4"));
        Integer source5 = Integer.parseInt(request.getParameter("source5"));

        //销量
        Integer orderCount = Integer.parseInt(request.getParameter("orderCount"));
        BigDecimal orderAmount =  new BigDecimal(request.getParameter("orderAmount"));

        String summary  = request.getParameter("summary");
        String improvement = request.getParameter("improvement");

        LiveDataReportEntity entity = new LiveDataReportEntity();
        entity.setCreateUserId(Integer.parseInt(userId.toString()));
        entity.setAccountId(mediaAccountId);
        entity.setCommentCount(commentCount);
        entity.setEndTime(endTime);
        entity.setImprovement(improvement);
        entity.setLiveDate(liveDate);
        entity.setNewUser(newUser);
        entity.setOrderAmount(orderAmount);
        entity.setOrderCount(orderCount);
        entity.setSource1(source1);
        entity.setSource2(source2);
        entity.setSource3(source3);
        entity.setSource4(source4);
        entity.setSource5(source5);
        entity.setStartTime(startTime);
        entity.setSummary(summary);
        entity.setViewCount(viewCount);

        reportService.add(entity);

        return "redirect:/live/data_report";
    }


    @RequestMapping(value = "/data_report_detail", method = RequestMethod.GET)
    public String detail(Model model, @RequestParam Integer id, HttpServletRequest request) {
        var report = reportService.getById(id);
        model.addAttribute("report", report);

        model.addAttribute("menuId", "live_data_report");
        return "live_data_report_detail";

    }

    @RequestMapping(value = "/data_report_detail", method = RequestMethod.POST)
    public String dataReportUpdate(Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        Object userId = request.getSession().getAttribute("userId");

        Integer id = Integer.parseInt(request.getParameter("id"));

        String liveDate= request.getParameter("liveDate");
        String startTime= request.getParameter("startTime");
        String endTime= request.getParameter("endTime");

        Integer viewCount = Integer.parseInt(request.getParameter("viewCount"));
        Integer newUser = Integer.parseInt(request.getParameter("newUser"));
        Integer commentCount = Integer.parseInt(request.getParameter("commentCount"));

        //流量来源
        Integer source1 = Integer.parseInt(request.getParameter("source1"));
        Integer source2 = Integer.parseInt(request.getParameter("source2"));
        Integer source3 = Integer.parseInt(request.getParameter("source3"));
        Integer source4 = Integer.parseInt(request.getParameter("source4"));
        Integer source5 = Integer.parseInt(request.getParameter("source5"));

        //销量
        Integer orderCount = Integer.parseInt(request.getParameter("orderCount"));
        BigDecimal orderAmount =  new BigDecimal(request.getParameter("orderAmount"));

        String summary  = request.getParameter("summary");
        String improvement = request.getParameter("improvement");

        LiveDataReportEntity entity = new LiveDataReportEntity();
        entity.setId(id);
        entity.setCommentCount(commentCount);
        entity.setEndTime(endTime);
        entity.setImprovement(improvement);
        entity.setLiveDate(liveDate);
        entity.setNewUser(newUser);
        entity.setOrderAmount(orderAmount);
        entity.setOrderCount(orderCount);
        entity.setSource1(source1);
        entity.setSource2(source2);
        entity.setSource3(source3);
        entity.setSource4(source4);
        entity.setSource5(source5);
        entity.setStartTime(startTime);
        entity.setSummary(summary);
        entity.setViewCount(viewCount);

        reportService.edit(entity);

        return "redirect:/live/data_report";
    }


    /**
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "/data_input_m", method = RequestMethod.GET)
    public String addm(Model model, HttpServletRequest request) {
        model.addAttribute("report", new LiveDataReportEntity());
        //查询客户列表
        List<MediaAccountEntity> mediaList = mediaAccountService.getList(1);
        model.addAttribute("mediaList", mediaList);

        model.addAttribute("menuId", "live_data_input");
        return "live_data_report_input_m";

    }


    @RequestMapping(value = "/data_report_detail_m", method = RequestMethod.GET)
    public String mDetail(Model model, @RequestParam Integer id, HttpServletRequest request) {
        var report = reportService.getById(id);
        model.addAttribute("report", report);

        model.addAttribute("menuId", "live_data_report");
        return "live_data_report_detail_m";

    }
}
